package br.great.DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import br.great.beans.Address;
import br.great.beans.Point;

/**
 * PhotoMap
 * Copyright (c) 2007-2008 STEAMER - LIG, All rights reserved.
 */
/**
 * @author Developpement of PhotoMap
 */
public class DAO_Address {

    /** method description
     * @param var signification
     * @exception exception class name
     * @return return_value return signification
     * @see package.class#member label
     */
    private static volatile DAO_Address instaceDAO_Address = new DAO_Address();

    /** The private constructor of DAO_Address for singleton
     */
    private DAO_Address() {
    }

    /** The method for get the instance of the class DAO_Address
     * @return DAO_Address instaceDAO_Address
     */
    public static DAO_Address getInstance() {
        //if(instaceDAO_Address==null)
        synchronized (DAO_Address.class) {
            if (instaceDAO_Address == null) {
                instaceDAO_Address = new DAO_Address();
            }
        }
        return instaceDAO_Address;
    }

    public Address getAddressByCoord(Point point) {
        PgConnect conn = PgConnect.getInstance();
        Address address = new Address();
        String getAddress = "SELECT c.id_commune, c.nom_commune, c.superficie,  c.status_commune,population, x(c.centre) as lat, y(c.centre) as lng," +
            "d.NOM_DEPARTEMENT,r.NOM_REGION,p.NAME_COUNTRY, " +
            "ST_Distance(c.CONTOUR,'POINT("+point.getLatitude()+" "+point.getLongitude()+")') as dis " +
            " FROM Region r,Commune c, Departement d, Country p " +
            " WHERE " +
            " (ST_covers(c.CONTOUR,'POINT("+point.getLatitude()+" "+point.getLongitude()+")') " +
            " OR" +
            " ST_Distance(c.CONTOUR,'POINT("+point.getLatitude()+" "+point.getLongitude()+")')<0.05)" +
            " AND r.ID_REGION = d.ID_REGION " +
            " AND d.ID_DEPARTEMENT= c.ID_DEPARTEMENT" +
            " AND p.ID_COUNTRY=r.ID_COUNTRY " +
            " ORDER BY dis;";
//System.out.println(getAddress);
        try {
            ResultSet rs = conn.executeQuery(getAddress);
            if (rs.next()) {
             //   System.out.println("Address trouve:" + rs.getString("nom_commune"));
                Point p = new Point();
                p.setLatitude(rs.getDouble("lat"));
                p.setLongitude(rs.getDouble("lng"));
                address.setCenter(p);

                address.setNameCity(rs.getString("nom_commune"));
                address.setNameCountry(rs.getString("name_country"));
                address.setNameDepartement(rs.getString("nom_departement"));
                address.setPopulation(rs.getInt("population")); 
                address.setStatus(rs.getString("status_commune"));
                address.setIdCityInDB(rs.getString("id_commune"));
                address.setNameRoad(" ");
            } else {
             //   System.out.println("Address non trouve");
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAO_Address.class.getName()).log(Level.SEVERE, null, ex);
        }
        return address;
    }
//    public static void main(String[] args){
//        DAO_Address da = DAO_Address.getInstance();
//        Point point = new Point();
//        point.setLatitude(45);
//        point.setLongitude(5);
//        Address a = da.getAddressByCoord(point);
//        
//        System.out.println(a.getNameCity());
//    }
}
